import 'package:bruno/bruno.dart';
import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:miaowu/app/module/auth/edit_info/user_tags/user_edit_tags_page.dart';
import 'package:miaowu/app/module/mine/audio_card/audio_card_record_restult_page.dart';
import 'package:miaowu/app/module/mine/money/integral/integral_home_page.dart';
import 'package:miaowu/app/module/mine/money/member_vip/member_page.dart';
import 'package:miaowu/app/module/mine/money/wallet/wallet_page.dart';

import '../../auth/edit_info/user_info_edit_info_all_page.dart';
import '../audio_card/record/audio_card_record_page.dart';
import '../kefu/kefu_main_page.dart';
import '../visitor_records/visitor_records_main_page.dart';
import 'about_me_page.dart';
import 'black_list_page.dart';
import 'cancel_account_application.dart';
import 'chat_record_settings_page.dart';
import 'chat_settings_page.dart';
import 'feekback_page/feekback_view.dart';
import 'message_notification_page.dart';
import 'privacy_settings_page.dart';
import 'setting_logic.dart';
import 'setting_state.dart';
import 'zan_notification_list_page.dart';

/// @description:
/// @author
/// @date: 2025-01-21 14:28:41
class SettingPage extends StatefulWidget {
  const SettingPage({Key? key}) : super(key: key);

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  final SettingLogic logic = Get.put(SettingLogic());

  final SettingState state = Get.find<SettingLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BrnAppBar(title: '系统设置', backgroundColor: Colors.white),
      backgroundColor: const Color(0xFFF6F6F6),
      body: GetBuilder<SettingLogic>(
          init: SettingLogic(),
          initState: (_) {},
          builder: (context) {
            return ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                String name = state.titles[index];
                String desc = '';
                if (name == '清除缓存') {
                  desc = state.appSize.toString();
                }
                if (name == '检查更新') {
                  desc = 'v ${ConfigService.to.version}';
                }
                return _buildItem(
                    title: name,
                    des: desc,
                    action: () {
                      List<String> titles = [
                        '消息通知',
                        '隐私设置',
                        '聊天设置',
                        '美颜设置',
                        '清除记录',
                        '黑名单',
                        '',
                        '清除缓存',
                        '检查更新',
                        '',
                        '意见反馈',
                        '关于我们',
                        '注销账号',
                        '系统客服'
                      ];
                      logs(titles);

                      if (name == '消息通知') {
                        Get.to(const MessageNotificationPage());
                      }
                      if (name == '隐私设置') {
                        Get.to(const PrivacySettingsPage());
                      }
                      if (name == '聊天设置') {
                        Get.to(const ChatSettingsPage());
                      }
                      if (name == '清除记录') {
                        Get.to(const ChatRecordSettingsPage());
                      }
                      if (name == '黑名单') {
                        Get.to(const BlackListPage());
                      }
                      if (name == '清除缓存') {
                        logic.clearCache();
                      }
                      if (name == '美颜设置') {}

                      if (name == '意见反馈') {
                        Get.to(FeekbackPage());
                      }
                      if (name == '关于我们') {
                        Get.to(const AboutMePage());
                      }
                      if (name == '注销账号') {
                        Get.to(const CancelAccountApplicationPage());
                      }
                      if (name == '系统客服') {
                        Get.to(const KeFuMainPage());
                      }
                      if (name == '访客记录') {
                        Get.to(const VisitorRecordsMainPage());
                      }
                      if (name == '点赞通知') {
                        Get.to(const ZanNotificationListPage());
                      }

                      if (name == '个人资料1') {
                        Get.to(const UserInfoEditInfoAllPage());
                      }
                      if (name == '声音卡片录制') {
                        Get.to(const AudioCardRecordPage());
                      }
                      if (name == '声音卡片') {
                        Get.to(const AudioCardRecordRestultPage());
                      }
                      if (name == '用户标签编辑') {
                        Get.to(const UserEditTagsPage());
                      }
                      if (name == '会员中心') {
                        Get.to(const MemberPage());
                      }
                      if (name == '金币余额') {
                        Get.to(const WalletPage());
                      }
                      if (name == '积分') {
                        Get.to(const IntegralHomePage());
                      }
                      if (name == '检查更新') {
                        SmartDialog.show(
                          usePenetrate: false,
                          clickMaskDismiss: true,
                          backType: SmartBackType.block,
                          keepSingle: true,
                          useAnimation: false,
                          builder: (context) => Center(
                            child: CustomAlertWidget(
                              title: '温馨提示',
                              content: "是否更新最新版本？\n",
                              confirmText: '确定',
                              confirmAction: () {},
                              cancemText: '取消',
                            ),
                          ),
                        );
                      }
                    });
              },
              separatorBuilder: (BuildContext context, int index) {
                return Divider(height: 0.5, color: const Color(0xFFEEEEEE).withOpacity(0), indent: 15, endIndent: 15);
              },
              itemCount: state.titles.length,
            );
          }),
      bottomNavigationBar: Container(
        padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom + 20),
        color: Colors.white,
        child: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            SmartDialog.show(
              usePenetrate: false,
              clickMaskDismiss: true,
              backType: SmartBackType.block,
              keepSingle: true,
              useAnimation: false,
              builder: (context) => Center(
                child: CustomAlertWidget(
                  title: '温馨提示',
                  content: "确认退出当前登录账号吗？\n",
                  confirmText: '确定',
                  confirmAction: () {},
                  cancemText: '取消',
                ),
              ),
            );
          },
          child: Container(
              alignment: Alignment.center,
              height: 60,
              child: Text("退出登录".tr,
                  style: const TextStyle(fontSize: 16, color: Color(0xFF000000), fontWeight: FontWeight.w500))),
        ),
      ),
    );
  }

  Widget _buildItem(
      {required String title, required String des, VoidCallback? action, bool isShowNextIcon = true, String? version}) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        action?.call();
      },
      child: Container(
        color: title.isNotEmpty ? Colors.white : Colors.transparent,
        padding: const EdgeInsets.symmetric(horizontal: 15),
        child: title.isNotEmpty
            ? Column(
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(vertical: 15),
                    child: Row(
                      children: [
                        Text(title.tr,
                            style: TextStyle(fontSize: 15.sp, color: Colors.black, fontWeight: FontWeight.w500)),
                        const Spacer(),
                        Visibility(
                            visible: des.isNotEmpty,
                            child: Text(des.tr, style: const TextStyle(fontSize: 14, color: Color(0xFF929C9B)))),
                        Visibility(
                            visible: version != null,
                            child: Text('-> ${version?.toString()}',
                                style: const TextStyle(
                                    fontSize: 14, color: Color(0xFFFF8139), fontWeight: FontWeight.bold))),
                        Visibility(
                            visible: isShowNextIcon,
                            child: const Icon(Icons.navigate_next_sharp, size: 20, color: Color(0xFF000000))),
                      ],
                    ),
                  ),
                  const Divider(color: Color(0xFFEEEEEE), height: 0.5, indent: 15, endIndent: 15)
                ],
              )
            : const SizedBox(height: 10),
      ),
    );
  }
}
